﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Text;
using System.IO;
using System.Xml;
using Maticsoft.DBUtility;

namespace StoreWeb.ajax
{
    public partial class purchaseManage : System.Web.UI.Page
    {
        BLL.PurchaseMain purchaseMainbll = new BLL.PurchaseMain();
        BLL.PurchaseDetail purchaseDetailbll = new BLL.PurchaseDetail();
        Maticsoft.BLL.UserAdmin user = new Maticsoft.BLL.UserAdmin();
        Maticsoft222.BLL.sqlexec sqlexec = new Maticsoft222.BLL.sqlexec();
        public string productname;
        protected void Page_Load(object sender, EventArgs e)
        {
  
            //if (!IsPostBack)
            //{
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")//作废
                {
                    Model.PurchaseMain ne = purchaseMainbll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.orderstatus = "已作废";
                    row = purchaseMainbll.Update(ne) ? 1 : 0;
                    Response.Write(row);
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "confirm")//审核
                {

                    //Model.PurchaseMain ne = purchaseMainbll.GetModel(Convert.ToInt32(Request["id"]));
                    //ne.orderstatus = "已审核";
                    //row = purchaseMainbll.Update(ne) ? 2 : 0;
                    //Response.Write(row);

                    Response.Write(sqlexec.ExecuteSql("exec STORE_TO_ERP_PurchaseMain '" + Request["id"] + "'") ? "1" : "");


                    //审核后 写入ERP销售订单SO_SOMain   SO_SODetails
                    //cSTCode销售类型编码  dDate受订日期  cSOCode销售订单号  cCusCode客户编码     cDepCode部门编码     cCusOAddress发货地址 
                    //cexch_name币种  iExchRate汇率   iTaxRate税率   iMoney定金   iStatus状态   cMaker制单人   bDisFlag是否整单打折  cDefine1自定义字段1  cCusName客户名称
                    //SO_SOMain(cSTCode,dDate,cSOCode,cCusCode,cDepCode,cCusOAddress,cexch_name,iExchRate,iTaxRate,iMoney,iStatus,cMaker,bDisFlag,cDefine1,cCusName)
	                //'00',createtime,@ordercode,DealersManageID,1,s_province+s_city+s_county+[address],'人民币',1,17,0.00,0,'demo',0,@codes,(select username from [DealersManage] where id=a.DealersManageid) from Orders a where code in (select * from [f_split](''+@codes+'',','))

                    //DbHelperSQLP db2 = new DbHelperSQLP("UFDATA_888_2016");
                    //insert into SO_SOMain(cSTCode,dDate,cSOCode,cCusCode,cDepCode,cCusOAddress,cexch_name,iExchRate,iTaxRate,iMoney,iStatus,cMaker,bDisFlag,cDefine1,cCusName)
                    //VALUES ('00',DateTime.Now,'2016212025','客户编码',1,'发货地址','人民币',1,17,0.00,0,'demo',0,'11','客户姓名')

                    //iSOsID销售订单子表ID号  cSOCode销售订单主表单据号   cInvCode存货编码   dPreDate预发日期   iQuantity数量   iNum件数   iQuotedPrice报价  
                    //insert into SO_SODetails(iSOsID,cSOCode,cInvCode,dPreDate,iQuantity,iNum,iQuotedPrice) 
                    //VALUES (111,'','',getdate(),1,1,5.2)

                    //Response.Write(db2.ExecuteSql("insert into SO_SOMain(cSTCode,dDate,cSOCode,cCusCode,cDepCode,cCusOAddress,cexch_name,iExchRate,iTaxRate,iMoney,iStatus,cMaker,bDisFlag,cDefine1,cCusName) VALUES ('00','" + DateTime.Now + "','2016212025','3',1,'发货地址2','人民币',1,17,0.00,0,'demo',0,'11','客户姓名')") > 0 ? "2" : "");
                   // Response.Write(db2.ExecuteSql("insert into SO_SODetails(iSOsID,cSOCode,cInvCode,dPreDate,iQuantity,iNum,iQuotedPrice)  VALUES (111,'0000000002','01010101001','" + DateTime.Now + "',1,1,5.2)") > 0 ? "2" : "");




                    
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "fh")//发货
                {

                    Model.PurchaseMain ne = purchaseMainbll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.expressinfo = "快递公司:" + Request["kdgs"] + "　快递单号:" + Request["kddh"];
                    ne.deliverytime = DateTime.Now;
                    ne.orderstatus = "已发货";
                    purchaseMainbll.Update(ne);
                    //row = purchaseMainbll.Update(ne) ? 2 : 0;
                    //Response.Write(row);
                    Response.Write(sqlexec.ExecuteSql("exec STORE_TO_ERP_PurchaseMainSEND '" + Request["id"] + "'") ? "2" : "");
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "sh")//确认收货
                {

                    Model.PurchaseMain ne = purchaseMainbll.GetModel(Convert.ToInt32(Request["id"]));
                    ne.orderstatus = "交易完成";
                    row = purchaseMainbll.Update(ne) ? 2 : 0;
                    Response.Write(row);
                }
                else
                {
                    Model.PurchaseMain ne = new Model.PurchaseMain();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        row = 3;
                    }
                    else
                    {


                            ne.createtime = DateTime.Now;
                            ne.ordercode = GetFormCode();
                            ne.productname = "";
                            ne.pricesum = Convert.ToDecimal(Request["totalprice"]);
                            ne.expressinfo = "";//快递信息
                            ne.deliverytime = null;//发货时间 
                            ne.orderstatus = "已付款";
                            ne.dealersid = user.GetModel(Convert.ToInt32(Request.Cookies["UserLogin"].Values[0])).dealersid;
                            int returnid = purchaseMainbll.Add(ne);
                            row = returnid > 0 ? 1 : 0;

                            string[] cpmcs = Request["cpmc"].ToString().Split(',');
                            string[] cpfls = Request["cpfl"].ToString().Split(',');
                            string[] unitprices = Request["unitprice"].ToString().Split(',');
                            string[] numbers = Request["number"].ToString().Split(',');
                            string[] sumprices = Request["sumprice"].ToString().Split(',');
                            for (int i = 0; i < cpmcs.Length; i++)
                            {
                                if (numbers[i].Trim() != "0")//数量不为0的才是用户选购的  才增加数据
                                {
                                    Model.PurchaseDetail p = new Model.PurchaseDetail();
                                    p.productname = cpmcs[i].Trim();
                                    productname +=cpmcs[i].Trim()+",";
                                    p.producttype = cpfls[i].Trim();
                                    p.price = Convert.ToDecimal(unitprices[i].Trim());
                                    p.nums = Convert.ToInt32(numbers[i].Trim());
                                    p.pricesum = Convert.ToDecimal(sumprices[i].Trim());
                                    p.purchaseMainid = returnid;
                                    purchaseDetailbll.Add(p);
                                }
                            }

                            ne = purchaseMainbll.GetModel(returnid);
                            ne.productname = productname.Substring(0, productname.Length - 1);
                            purchaseMainbll.Update(ne);
                        


                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else if (row == 3)
                    {
                        Response.Write("3");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
           // }
        }



        #region 生成单据号
        /// <summary> 
        /// 生成单据号 
        /// </summary> 
        /// <param name="pFromType"></param> 
        /// <returns></returns> 
        public static string GetFormCode()
        {
            string formcode = "CG";
            //switch (pFromType)
            //{
            //    case FormType.CaiGou:
            //        {
            //            formcode = "CG";
            //            break;
            //        }
            //    case FormType.ChaiXie:
            //        {
            //            formcode = "CX";
            //            break;
            //        }
            //    case FormType.ChuKu:
            //        {
            //            formcode = "CK";
            //            break;
            //        }
            //    case FormType.LingLiao:
            //        {
            //            formcode = "LL";
            //            break;
            //        }
            //    case FormType.PeiChang:
            //        {
            //            formcode = "PC";
            //            break;
            //        }
            //    case FormType.RuKu:
            //        {
            //            formcode = "RK";
            //            break;
            //        }
            //    case FormType.ShengChan:
            //        {
            //            formcode = "SC";
            //            break;
            //        }
            //    case FormType.SunYi:
            //        {
            //            formcode = "SY";
            //            break;
            //        }
            //    case FormType.TuiHuo:
            //        {
            //            formcode = "TH";
            //            break;
            //        }
            //    case FormType.XiaoShou:
            //        {
            //            formcode = "XS";
            //            break;
            //        }
            //    case FormType.PeiSong:
            //        {
            //            formcode = "PS";
            //            break;
            //        }
            //    case FormType.ZengPin:
            //        {
            //            formcode = "ZP";
            //            break;
            //        }
            //    case FormType.FuKuan:
            //        {
            //            formcode = "FK";
            //            break;
            //        }
            //    case FormType.ShouKuan:
            //        {
            //            formcode = "SK";
            //            break;
            //        }
            //}
            formcode += DateTime.Now.Year.ToString();
            formcode += DateTime.Now.Month.ToString().Length == 1 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString();
            formcode += DateTime.Now.Day.ToString().Length == 1 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString();
            formcode += DateTime.Now.Hour.ToString().Length == 1 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
            formcode += DateTime.Now.Minute.ToString().Length == 1 ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
            formcode += DateTime.Now.Second.ToString().Length == 1 ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
            if (DateTime.Now.Millisecond.ToString().Length == 1)
            {
                formcode += "00" + DateTime.Now.Millisecond.ToString();
            }
            else if (DateTime.Now.Millisecond.ToString().Length == 2)
            {
                formcode += "0" + DateTime.Now.Millisecond.ToString();
            }
            else
            {
                formcode += DateTime.Now.Millisecond.ToString();
            }
            return formcode;
        }
        #endregion 


    }
}